package tech.summerly.weather.component.adapter.floors

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import kotlinx.android.synthetic.main.floor_realtime_weather.view.*
import tech.summerly.weather.R
import tech.summerly.weather.component.activities.AqiDetailActivity
import tech.summerly.weather.component.fragments.LiveWeatherFragment
import tech.summerly.weather.data.bean.RealTime
import tech.summerly.weather.data.remote.CaiYunService
import tech.summerly.weather.util.*
import java.util.*

class RealTimeFloor : BaseFloor<RealTime>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return createHolder(R.layout.floor_realtime_weather, inflater, parent)
    }

    override fun onBindViewHolder(holder: ViewHolder, item: RealTime) = with(holder.itemView) {
        textCity.text = item.region?.name
        val today = today()
        textDate.text = context.getString(R.string.template_date, today[Calendar.MONTH] + 1, today[Calendar.DAY_OF_MONTH])


        textTemperature.text = context.getString(R.string.template_celsius, item.temperature.toIntString())
        textQuality.text = string(R.string.template_aqi, Aqi.from(item.aqi).localString())
        textSky.text = item.getSkyConString()
        imageSky.setImageResource(CaiYunService.SkyCon.getSkyIcon(item.skycon))
        setBackgroundColor(item.getSkyCon().getBackgroundColor())
        textQuality.setOnClickListener {
            val intent = Intent(context, AqiDetailActivity::class.java)
            intent.putExtra(AqiDetailActivity.ARG_REGION, item.region!!)
            context.startActivity(intent)
        }
        imageLiveWeather.setOnClickListener {
            val region = item.region ?: return@setOnClickListener
            getTowerAdapter().activity.supportFragmentManager.inTransaction {
                val instance = LiveWeatherFragment.newInstance(region)
                add(android.R.id.content, instance, LiveWeatherFragment.TAG)
                addToBackStack(LiveWeatherFragment.TAG)
            }
        }
    }

}